package com.wyg.service.impl;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.wyg.model.User;
import com.wyg.service.UserService;
import com.wyg.utils.SentinelUtil;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

import static com.wyg.utils.SentinelUtil.sleep;

/**
 * @description:
 * @author: WYG
 * @time: 2020/10/24 16:55
 */

public class UserServiceImpl implements UserService {

    private static Map<Integer,User> userMap = new HashMap<>();

    static {
        userMap.put(1, new User(1,"wyg","123456","hust","123456","boy",23));
        userMap.put(2, new User(1,"wdn","123456","hust","123456","girl",23));
        userMap.put(3, new User(1,"wz","123456","hust","123456","boy",23));
        userMap.put(4, new User(1,"wjh","123456","hust","123456","boy",12));
        userMap.put(5, new User(1,"wld","123456","hust","123456","girl",16));
    }

    @Override
    public User findUserById(int id) throws Throwable {
        SentinelUtil.initFlowRules("findUserById");
        User user = null;
        try (Entry entry = SphU.entry("findUserById")) {
            user = userMap.get(id);
        } catch (BlockException e) {
            System.out.println("服务崩溃");
            throw new Throwable("Service down");
        }
        return user;
    }

    @Override
    public List<User> getUsers() throws Throwable {
        SentinelUtil.initDegradeRule("getUsers");
        List<User> users = new ArrayList<>();
        try (Entry entry = SphU.entry("getUsers")) {
            for (Map.Entry<Integer, User> userEntry : userMap.entrySet()) {
                users.add(userEntry.getValue());
            }
            int x = 1/0;
        } catch (Throwable t) {
            if (!BlockException.isBlockException(t)) {
                Tracer.trace(t);
            }
            System.out.println("熔断");
        }
        return users;
    }

    @Override
    public int addUser(User user) throws Throwable {
        SentinelUtil.initFlowRules("addUser");
        try (Entry entry = SphU.entry("addUser")) {
            userMap.put(userMap.size()+1, user);
        } catch (BlockException e) {
            System.out.println("服务崩溃");
            throw new Throwable("服务限流");
        }
        return 1;
    }

    @Override
    public int deleteUser(int id) throws Throwable {
        SentinelUtil.initFlowRules("deleteUser");
        try (Entry entry = SphU.entry("deleteUser")) {
            userMap.remove(id);
        } catch (BlockException e) {
            System.out.println("服务崩溃");
            throw new Throwable("服务限流");
        }
        return 1;
    }

    @Override
    public int updateUser(User user) throws Throwable {
        SentinelUtil.initFlowRules("updateUser");
        try (Entry entry = SphU.entry("updateUser")) {
            userMap.put(user.getId(), user);
        } catch (BlockException e) {
            System.out.println("服务崩溃");
            throw new Throwable("服务限流");
        }
        return 1;
    }
}
